package de.heimlich.gut;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class WidgetPreferences {
	

	private static final String PREFS_NAME = "MessagesWidgetPreferences";
	public static final String POSITION = "SmsPosition";
	public static final String TAP_BEHAVIOR = "TapBehavior";
	public static final String SHOW = "Show";
	public static final String LAYOUT = "Layout";
	
	private SharedPreferences prefs;
	private Editor editor = null;
	
	public WidgetPreferences(Context context) {
		prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_WORLD_WRITEABLE);
	}
	
	protected Editor getEditor() {
		if(editor == null) {
			editor = prefs.edit();
		}
		return editor;
	}
	
	public void setPosition(int position, int widgetId) {
		Editor editor = getEditor();
		editor.putInt(widgetId + POSITION, position);
		editor.commit();
	}
	
	public int getPosition(int widgetId) {
		return prefs.getInt(widgetId + POSITION, 0);
	}
	
	public void setLayout(int layout, int widgetId) {
		Editor editor = getEditor(); 
		editor.putInt(widgetId + LAYOUT, layout);
		editor.commit();
	}
	
	public int getLayout(int widgetId) {
		return prefs.getInt(widgetId + LAYOUT, R.layout.main);
	}
	
	
	
	public void setTapBehavior(int tapBehavior, int widgetId) {
		Editor editor = getEditor();
		editor.putInt(widgetId + TAP_BEHAVIOR, tapBehavior);
		editor.commit();
	}
	
	public int getTapBehavior(int widgetId) {
		return prefs.getInt(widgetId + TAP_BEHAVIOR, R.id.config_tap_popup);
	}
	
	public void setShow(int show, int widgetId) {
		Editor editor = getEditor();
		editor.putInt(widgetId + SHOW, show);
		editor.commit();
	}
	
	public int getShow(int widgetId) {
		return prefs.getInt(widgetId + SHOW, R.id.config_show_inbox);
	}
	
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		if(editor != null) editor.commit();
	}
	
}
